﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

/// <summary>
/// Summary description for GenDataTable
/// </summary>
public static class GenDataTable
{
	
    public static string GetControlsNameForPanel(Panel pn)
    {
        return GetControlNameFrom(GetControlFrom(pn));
    }
    public static string GetDummyForPanel(Panel pn) 
    {
        return GetDataTableDummy(GetControlFrom(pn));
    }
    public static Control[] GetControlFrom(Panel pn)
    {
        List<Control> listControls = new List<Control>();
        foreach (Control c in pn.Controls)
        {
            if (c.ID != null)
            {
                if (c is Panel)
                {
                    Control[] cn = GetControlFrom((Panel)c);
                    foreach (Control cx in cn)
                    {
                        listControls.Add(cx);
                    }
                } if (c is UpdatePanel)
                {

                    UpdatePanel ctrlPanel = (UpdatePanel)c;
                    if (ctrlPanel.HasControls())
                    {

                        foreach (Control subCtrl in ctrlPanel.Controls[0].Controls)
                        {
                            if (subCtrl is LiteralControl)
                                continue;
                            else if (subCtrl is Panel)
                            {
                                Control[] cn = GetControlFrom((UpdatePanel)c);
                                foreach (Control cx in cn)
                                {
                                    listControls.Add(cx);
                                }
                            }
                            else
                                listControls.Add(subCtrl);
                        }
                    }

                }
                else
                    listControls.Add(c);
            }

        }
        return (Control[])listControls.ToArray();
    }
    public static Control[] GetControlFrom(UpdatePanel pn)
    {
        List<Control> listControls = new List<Control>();
        foreach (Control c in pn.Controls)
        {
            if (c.ID != null)
            {
                if (c is UpdatePanel)
                {
                    Control[] cn = GetControlFrom((UpdatePanel)c);
                    foreach (Control cx in cn)
                    {
                        listControls.Add(cx);
                    }
                }
                else
                    listControls.Add(c);
            }

        }
        return (Control[])listControls.ToArray();
    }
    public static string GetControlNameFrom(Control[] pn)
    {
        ArrayList str = new ArrayList(); 
        DataTable newDataTable = new DataTable(); 
        DataRow newDataRows = newDataTable.NewRow();
        string textSt = "";
        textSt += @"DataTable newDataTable = new DataTable();";
        textSt += " ";
        foreach(Control c in pn)
        {
            if (c.GetType() == typeof(ImageButton) || GetStringFindControlValue(c) == "")
            {
                continue;
            }
            textSt += "newDataTable.Columns.Add(\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\");";
            textSt += " ";
        }
        //string[] newStr = (string[])str.ToArray();
        

        textSt += "DataRow newDataRows = newDataTable.NewRow();";
        textSt += " ";
        foreach (Control c in pn)
        {
            if (c.GetType() == typeof(ImageButton) || GetStringFindControlValue(c)=="")
            {
                continue;
            }
            textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = " + GetStringFindControlValue(c) + ";";
            textSt += " ";
        }
        textSt += "newDataTable.Rows.Add(newDataRows);";
        return textSt;
        //MsgBox(textSt);
    }

    public static string GetDummyFromPanel(Control[] pn)
    {
        ArrayList str = new ArrayList();
        DataTable newDataTable = new DataTable();
        DataRow newDataRows = newDataTable.NewRow();
        string textSt = "";
        textSt += @"DataTable newDataTable = new DataTable();";
        textSt += " ";
        foreach (Control c in pn)
        {
            if (c.GetType() == typeof(ImageButton) || GetStringFindControlValue(c) == "")
            {
                continue;
            }
            textSt += "newDataTable.Columns.Add(\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\");";
            textSt += " ";
        }
        //string[] newStr = (string[])str.ToArray();


        textSt += "DataRow newDataRows = newDataTable.NewRow();";
        textSt += " ";
        int i = 0;
        foreach (Control c in pn)
        {
            if (c.GetType() == typeof(ImageButton) || GetStringFindControlValue(c) == "")
            {
                continue;
            }
            textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = " + i + ";";
            textSt += " ";
            i++;
        }
        textSt += "newDataTable.Rows.Add(newDataRows);";
        return textSt;
        //MsgBox(textSt);
    }

    public static string GetDataTableDummy(Control[] pn)
    {
        ArrayList str = new ArrayList();
        DataTable newDataTable = new DataTable();
        DataRow newDataRows = newDataTable.NewRow();
        string textSt = "";
        textSt += @"DataTable newDataTable = new DataTable();";
        textSt += " ";
        foreach (Control c in pn)
        {
            if (c.GetType() == typeof(ImageButton) || GetStringFindControlValue(c) == "")
            {
                continue;
            }
            textSt += "newDataTable.Columns.Add(\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\");";
            textSt += " ";
        }
        //string[] newStr = (string[])str.ToArray();


        textSt += "DataRow newDataRows = newDataTable.NewRow();";
        textSt += " ";
        foreach (Control c in pn)
        {
            if (c.GetType() == typeof(ImageButton) || GetStringFindControlValue(c) == "")
            {
                continue;
            }
            if(c.ID.ToLower().IndexOf("id")>-1)
            {
                textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = -1;";
                textSt += " ";
            }
            else if (c.ID.ToLower().IndexOf("chk") > -1)
            {
                textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = false;";
                textSt += " ";
            }
            else if (c.ID.ToLower().IndexOf("text") > -1)
            {
                textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = \"random " + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\";";
                textSt += " ";
            }
            else if (c.ID.ToLower().IndexOf("date") > -1)
            {
                textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = DateTime.Now;";
                textSt += " ";
            }
            else
            {
                textSt += "newDataRows[\"" + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\"] = \"random " + c.ID.Substring(c.ID.IndexOf('_') + 1) + "\";";
                textSt += " ";
            }
        }
        textSt += "newDataTable.Rows.Add(newDataRows);";
        return textSt;
        //MsgBox(textSt);
    }
    public static void MsgBox(string msg)
        {
            HttpContext.Current.Session["alertDataRows"] = msg;
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                
                Util.jsWriter(page, "rwrwr", "window.open('Default2.aspx?x='" + new Random(1).Next() + ")");
                //SDS_Util.jsAlert(page, msg);
            }

        }
    public static string GetStringFindControlValue(Control ControlTypeName)
    {

        if (ControlTypeName.GetType() == typeof(TextBox))
        {
            return "((TextBox)" + ControlTypeName .ID+ ").Text.Trim()";
        }
        else if (ControlTypeName.GetType() == typeof(CheckBox))
        {
            return "((CheckBox)" + ControlTypeName.ID + ").Checked.ToString()";
        }
        else if (ControlTypeName.GetType() == typeof(RadioButtonList))
        {
            return "((RadioButtonList)" + ControlTypeName.ID + ").SelectedValue.ToString()";
        }
        else if (ControlTypeName.GetType() == typeof(RadioButton))
        {
            return "((RadioButton)" + ControlTypeName.ID + ").Checked.ToString()";
        }
        else if (ControlTypeName.GetType() == typeof(Label))
        {
            return "((Label)" + ControlTypeName.ID + ").Text.ToString()";
        }
        else if (ControlTypeName.GetType() == typeof(DropDownList))
        {
            return "((DropDownList)" + ControlTypeName.ID + ").SelectedValue.ToString()";
        }
        else
        {
            return "";
        }
        return "";
    }
    public static string FindControlValue(Control ControlTypeName) 
    {

            if (ControlTypeName.GetType()==typeof(TextBox))
            {
                return ((TextBox)ControlTypeName).Text.Trim() == "" ? "0" : ((TextBox)ControlTypeName).Text;
            }
            else if(ControlTypeName.GetType()==typeof(CheckBox))
            {
                return ((CheckBox)ControlTypeName).Checked.ToString();
            }
            else if(ControlTypeName.GetType()==typeof(RadioButtonList))
            {
                return ((RadioButtonList)ControlTypeName).SelectedValue.ToString();
            }
            else if (ControlTypeName.GetType() == typeof(RadioButton))
            {
                return ((RadioButton)ControlTypeName).Checked.ToString();
            }
            else
            {
                return null;
            }
        return null;  
    }
    public static Control[] GetControlFrom(Content pn)
    {
        List<Control> listControls = new List<Control>();
        foreach (Control c in pn.Controls)
        {
            if (c.ID != null)
            {
                if (c is Panel)
                {
                    Control[] cn = GetControlFrom((Panel)c);
                    foreach (Control cx in cn)
                    {
                        listControls.Add(cx);
                    }
                }
                else
                    listControls.Add(c);
            }

        }
        return (Control[])listControls.ToArray();
    }
    public static string WriteDummy(Object obj, string objName)
    {
        Type t = obj.GetType();
        PropertyInfo[] pi = t.GetProperties();
        string text = "";
        Random rd = new Random();
        foreach (PropertyInfo p in pi)
        {
            if (p.PropertyType.FullName.IndexOf("Int") > 1)
            {
                text += objName + "." + p.Name + " = " + "-1;" + "</br>";
            }
            else if (p.PropertyType.FullName.IndexOf("String") > 1)
            {
                text += objName + "." + p.Name + " = " + "\"" + rd.Next() + "\";" + "</br>";
            }
            else if (p.PropertyType.FullName.IndexOf("Boolean") > 1)
            {
                text += objName + "." + p.Name + " = " + "false;" + "</br>";
            }
            else if (p.PropertyType.FullName.IndexOf("DateTime") > 1)
            {
                text += objName + "." + p.Name + " = " + "DateTime.Now;" + "</br>";
            }
        }
        return text;
    }
}
